<template>
  <el-drawer
    :wrapperClosable="false"
    direction="rtl"
    custom-class="demo-drawer"
    :title="title"
    ref="drawer"
    size="40%"
    :visible.sync="dialogFormVisible"
  >
    <div class="demo-drawer__content">
      <el-form ref="form" label-width="auto" :model="form" :rules="rules">
        <el-form-item label="用户名:" prop="userName">
          <el-input v-model="form.userName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机号:" prop="userTel">
          <el-input v-model="form.userTel" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div class="demo-drawer__footer">
        <el-button @click="close" type="warning">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
  import { SetUserInfo, SysAddUserInfo } from '@/api/userManagement'

  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        departmentIdlist: [],
        Dpmentlist: [],
        form: {
          userName: '',
          userTel: '',
        },
        rules: {
          userName: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
          userTel: [{ required: true, trigger: 'blur', message: '请输入用户手机号' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    watch: {
      departmentIdlist(newvel) {
        this.form.departmentId = newvel.join(',')
      },
    },
    created() {},
    methods: {
      changedpment(e) {},
      showEdit(row) {
        if (!row) {
          this.title = '新增用户'
        } else {
          this.title = '编辑用户信息'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = this.title === '新增用户' ? await SysAddUserInfo(this.form) : await SetUserInfo(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  ::v-deep .demo-drawer__content {
    height: 100%;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    .demo-drawer__footer {
      bottom: 0;
    }
  }
</style>
